package controller;

import entity.AcademicScores;
import service.AcademicScoresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/academicscores")
public class AcademicScoresController {

    @Autowired
    private AcademicScoresService academicScoresService;

    //  添加学业成绩
    @PostMapping
    public Map<String, Object> create(@RequestBody AcademicScores obj) {
        int result = academicScoresService.create(obj);
        if (result > 0) {
            return Map.of("code", 200, "message", "添加成功");
        } else {
            return Map.of("code", 500, "message", "添加失败");
        }
    }

    //  查询单条记录
    @GetMapping("/{id}")
    public AcademicScores getById(@PathVariable Integer id) {
        return academicScoresService.getById(id);
    }

    //  查询所有记录
    @GetMapping("/all")
    public List<AcademicScores> getAll() {
        return academicScoresService.getAll();
    }

    //  查询某个学生的成绩列表
    @GetMapping("/student/{studentId}")
    public List<AcademicScores> getByStudentId(@PathVariable Integer studentId) {
        return academicScoresService.getByStudentId(studentId);
    }

    //  修改成绩
    @PutMapping
    public Map<String, Object> update(@RequestBody AcademicScores obj) {
        int result = academicScoresService.update(obj);
        if (result > 0) {
            return Map.of("code", 200, "message", "修改成功");
        } else {
            return Map.of("code", 500, "message", "修改失败");
        }
    }

    // 删除成绩
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        int result = academicScoresService.delete(id);
        if (result > 0) {
            return Map.of("code", 200, "message", "删除成功");
        } else {
            return Map.of("code", 500, "message", "删除失败");
        }
    }
}
